/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Author: Daniel Stegmuller
Date: February 2023
Last modified: Sept 7 2023
Object: Produce unobserved heterogeneity with correlated random effects table in the OA
Databases in input: A_experiment.dta
Databases in output: None

Tables generated in this script:

- Table E.13.: Instrumental variables estimates allowing for treatment effect heterogeneity in first and second stage.

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ****/


qui do 2_code/_ivcrc_estimator.ado
qui do 2_code/ivcrc.ado

set matafavor speed
set maxiter 40
set seed 782526

* N bootstrap iter.
local rep = 500



**************************************************************************
* Satisfaction with the head of government
**************************************************************************

use 1_data/A_experiment, replace

gen fifty = (fifties == 1| sixties == 1 | seventies == 1)
local ctrl = "female income2quartile income3quartile income4quartile goodhealth fifty"

eststo clear 
* M 1
loc bw = 0.0195983
eststo: ivcrc satis_head (eval_eco eval_sant = TH1_TE1 - TH4_TE4) `ctrl', ranks(5) kernel(epanechnikov) bandw(`bw') ave(0(0)1) bootstrap(rep(`rep') cluster(country))
estadd scalar bw = `bw'
estadd local iv "16 IVs"

* M 2
loc bw = 0.0268942
eststo: ivcrc satis_head (eval_eco eval_sant = econc healthc) `ctrl', ranks(5) kernel(epanechnikov) bandw(`bw') ave(0(0)1) bootstrap(rep(`rep') cluster(country))
estadd scalar bw = `bw'
estadd local iv "2 SumIVs"


**************************************************************************
* Satisfaction with democracy
**************************************************************************

use 1_data/A_experiment, replace
gen fifty = (fifties == 1| sixties == 1 | seventies == 1)
local ctrl = "female income2quartile income3quartile income4quartile goodhealth fifty"

* M 3
loc bw = 0.0192313
eststo: ivcrc satis_dem (satis_head = TH1_TE1 - TH4_TE4) `ctrl', ranks(5) kernel(epanechnikov) bandw(`bw') bootstrap(rep(`rep') cluster(country))
estadd scalar bw = `bw'
estadd local iv "16 IVs"

* M 4
loc bw = 0.016551
eststo: ivcrc satis_dem (satis_head = econc healthc) `ctrl', ranks(5) kernel(epanechnikov) bandw(`bw') bootstrap(rep(`rep') cluster(country))
estadd scalar bw = `bw'
estadd local iv "2 SumIVs"

* M 5
loc bw = 0.0163449
eststo: ivcrc satis_dem (satis_head = treatc) `ctrl', ranks(5) kernel(epanechnikov) bandw(`bw') bootstrap(rep(`rep') cluster(country))
estadd scalar bw = `bw'
estadd local iv "SumIV"


esttab  using "3_output/2_OA/Tables/TableE13.tex", cells(b(star fmt(%5.3fc)) se(par fmt(%5.3fc)))  varlabels(eval_eco "Economic satisfaction" eval_sant "Health satisfaction" satis_head "Satisfaction with the head of government") keep(eval_eco eval_sant satis_head) stats(iv bw N, fmt(%15s %5.4f %9.0fc) labels("Instruments" "Bandwidth \emph{h}" "Observations")) numbers mlabels(,none) collabels(,none) eqlabels(, none)  mgroups("Satisfaction with the head of government" "Satisfaction with democracy", pattern(1 0 1 0 0 ) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) replace star(* 0.10 ** 0.05 *** 0.01)
